package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAgentUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AgentUpdateLogic {
	return &AgentUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改代理商
func (l *AgentUpdateLogic) AgentUpdate(in *agentClient.AgentParam) (*agentClient.EmptyBody, error) {
	agentInfo, err := l.svcCtx.AgentModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}

	agentInfo.Avatar = in.Avatar
	agentInfo.Name = in.Name
	agentInfo.Gender = in.Gender
	agentInfo.Phone = in.Phone
	agentInfo.Status = in.Status
	agentInfo.Type = in.Type
	agentInfo.IdCard = in.IdCard
	agentInfo.RealName = in.RealName
	agentInfo.Company = in.Company
	agentInfo.BusinessLicense = in.BusinessLicense
	agentInfo.UnifiedSocialCreditCode = in.UnifiedSocialCreditCode
	agentInfo.UpdateTime = time.Now()
	agentInfo.UpdateBy = in.UpdateBy
	agentInfo.DelFlag = in.DelFlag
	agentInfo.OpenId = in.OpenId
	agentInfo.UnionId = in.UnionId
	agentInfo.StoreNum = in.StoreNum
	agentInfo.ProfitRate = in.ProfitRate
	agentInfo.CardRate = in.CardRate
	agentInfo.ParentUid = in.ParentUid

	err = l.svcCtx.AgentModel.Update(l.ctx, agentInfo)
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}
	return &agentClient.EmptyBody{}, nil
}
